<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 100%; overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel"> 关闭 </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工地名称">
            <a-input placeholder="请输入工地名称" v-decorator.trim="['name']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工点位置">
            <a-input placeholder="请输入工点位置"  v-decorator.trim="['address']"  />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import {addEngineering,checkSupplier,findBySelectCus,addManageList,updateManageList } from '@/api/api'
  import JDate from '@/components/jeecg/JDate'
  export default {
    name: "projectListModal",
    components: {
      JDate
    },
    inject:["reload"],
    props: ['id'],
    data () {
      return {
        ids:'',
        arrList:[],
        title:"操作",
        visible: false,
        model: {},
        record:{},
        operTimeStr: '',
        isReadOnly: false,
        arr:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          supplier:{
            rules: [
              { required: true, message: '请输入名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
              { validator: this.validateSupplierName}
            ]
          }
        },
        url: {
          // add: '/engineering/addEngineering',
          add: '/engineeringPoint/add',
          edit: '/engineeringPoint/update',
          detailList: '/depotItem/getDetailList'
        }
      }
    },
    created () {
    },
    mounted(){
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        console.log('edit',record)
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.record = record;
        console.log('edit',record.id, this.model.id)
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','address' ))
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
         // console.log(this.model,8888)
       //   console.log(values,9999)
          if (!err) {
            that.confirmLoading = true;
            console.log('formData.engineeringId', that.id,that.model.id)
            let formData = Object.assign(that.model, values);

            formData.engineeringId = this.id;
            let obj;
            if(!that.record.id){
              obj=addManageList(formData);
            }else{
              formData.id = that.record.id;
              obj=updateManageList(formData);
            }
            obj.then((res)=>{
              if(res.code === 0){
                // that.$emit('成功');
                that.$message.success('成功');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            //  this.reload();
              
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateSupplierName(rule, value, callback){
        let params = {
          name: value,
          id: this.model.id?this.model.id:0
        };
        checkSupplier(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data.status){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }
    }
  }
</script>
<style scoped>
.ant-calendar-picker {
  width: 250px;
}
</style>